
import {computed, ref} from "vue";
import Activity from "@/views/crm/componentsNew/ActivityNew/index.vue";
import CRMEditBaseInfo from "@/views/crm/componentsNew/BaseInfo/CRMEditBaseInfo.vue";
import RelativeContacts from "@/views/crm/componentsNew/relative/RelativeContacts.vue";
import RelativeTeam from "@/views/crm/componentsNew/relative/RelativeTeam.vue";
import RelativeBusiness from "@/views/crm/componentsNew/relative/RelativeBusiness.vue";
import RelativeContract from "@/views/crm/componentsNew/relative/RelativeContract.vue";
import RelativeReturnMoney from "@/views/crm/componentsNew/relative/RelativeReturnMoney.vue";
import RelativeVisit from "@/views/crm/componentsNew/relative/RelativeVisit.vue";
import RelativeInvoice from "@/views/crm/componentsNew/relative/RelativeInvoice.vue";
import RelativeFiles from "@/views/crm/componentsNew/relative/RelativeFiles.vue";
import {useErpUserStore} from "@/store/modules/usererp.js";
import {crmLeadsNumAPI} from "@/api/crm/leads.js";
import {crmCustomerNumAPI} from "@/api/crm/customer.js";
import {crmContactsNumAPI} from "@/api/crm/contacts.js";
import {crmBusinessNumAPI} from "@/api/crm/business.js";
import {crmContractNumAPI} from "@/api/crm/contract.js";
import {crmProductNumAPI} from "@/api/crm/product.js";
import {crmReceivablesNumAPI} from "@/api/crm/receivables.js";


export function useTabsHandleHook(crmType,customerId) {

// 从store中拿到crm权限
  const crm = computed(() => {
    const erpUserStore = useErpUserStore();
    return erpUserStore.crm
  })

  const showTabsNumber = computed(() => {
    return crmType !== 'visit'
  })

  const tabOption = reactive(({
    tabsNumber: {}
  }))

  const tabNames = computed(() => {
    let tempsTabs = []
    tempsTabs.push({label: '活动', name: 'Activity', comp: Activity})
    if (crm.value.customer && crm.value.customer.read) {
      tempsTabs.push({label: '详细资料', name: 'CRMEditBaseInfo', comp: CRMEditBaseInfo})
    }
    if (crm.value.contacts && crm.value.contacts.index) {
      tempsTabs.push({
        label: getTabName('联系人', tabOption.tabsNumber.contactCount),
        name: 'RelativeContacts',
        comp: RelativeContacts
      })
    }

    tempsTabs.push({
      label: getTabName('团队成员', tabOption.tabsNumber.memberCount),
      name: 'RelativeTeam',
      comp: RelativeTeam
    })

    if (crm.value.business && crm.value.business.index) {
      tempsTabs.push({
        label: getTabName('商机', tabOption.tabsNumber.businessCount),
        name: 'RelativeBusiness',
        comp: RelativeBusiness
      })
    }

    if (crm.value.contract && crm.value.contract.index) {
      tempsTabs.push({
        label: getTabName('合同', tabOption.tabsNumber.contractCount),
        name: 'RelativeContract',
        comp: RelativeContract
      })
    }
    if ((crm.value.receivables && crm.value.receivables.index) || (crm.value.receivablesPlan && crm.value.receivablesPlan.index)) {
      tempsTabs.push({
        label: getTabName('回款', tabOption.tabsNumber.receivablesCount),
        name: 'RelativeReturnMoney',
        comp: RelativeReturnMoney
      })
    }
    if (crm.value.visit && crm.value.visit.index) {
      tempsTabs.push({
        label: getTabName('回访', tabOption.tabsNumber.returnVisitCount),
        name: 'RelativeVisit',
        comp: RelativeVisit
      })
    }

    if (crm.value.invoice && crm.value.invoice.index) {
      tempsTabs.push({
        label: getTabName('发票', tabOption.tabsNumber.invoiceCount),
        name: 'RelativeInvoice',
        comp: RelativeInvoice
      })
    }

    tempsTabs.push({label: getTabName('附件', tabOption.tabsNumber.fileCount), name: 'RelativeFiles', comp: RelativeFiles})
    tempsTabs.push({label: '操作记录', name: 'RelativeHandle'})
    return tempsTabs
  })
  /**
   * 获取tabs名字
   * @param {*} name
   * @param {*} num
   */
   const getTabName = (name, num) => {
    return `${name}${num && num > 0 ? '（' + num + '）' : ''}`
  }
  /**
   * 获取tab数量
   */
   const getTabsNum = () => {
    if (!showTabsNumber.value) {
      return
    }
    const request = {
      leads: crmLeadsNumAPI,
      customer: crmCustomerNumAPI,
      contacts: crmContactsNumAPI,
      business: crmBusinessNumAPI,
      contract: crmContractNumAPI,
      product: crmProductNumAPI,
      receivables: crmReceivablesNumAPI
    }[crmType]

    if (!request) {
      return
    }

    const params = {}
    params[`id`] = customerId
    console.log("aaaaaaaaaaaaaaaaaaaaa", params)
    request(params).then(res => {
      console.log("aaaaaaaaaaaaaaaaaaaaa", res)
      tabOption.tabsNumber = res.data.data || {}
    }).catch(() => {
    })
  }

  return {
    tabOption,showTabsNumber,tabNames,getTabName,getTabsNum
  }
}